package com.example.partyfee.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;

/**
 * 系统启动时初始化管理员用户
 */
@Slf4j
@Component
public class AdminInitializer implements CommandLineRunner {

    @Autowired
    private DataSource dataSource;

    @Override
    public void run(String... args) {
        try {
            log.info("开始初始化管理员用户...");
            ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
            populator.addScript(new ClassPathResource("db/init-admin.sql"));
            populator.execute(dataSource);
            log.info("管理员用户初始化完成");
        } catch (Exception e) {
            log.error("管理员用户初始化失败", e);
        }
    }
} 